package com.shop.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shop.Utils.ResponseMessage;
import com.shop.exception.zdyException;
import com.shop.po.Adminuser;
import com.shop.po.User;
import com.shop.service.UserService;

@Controller
public class loginController {

	@Autowired
	private UserService userService;

	@RequestMapping("/login")
	public String login() {
		return "login";
	}

	// 用户登录
	@RequestMapping("/userLogin")
	public String userLogin(HttpServletRequest request, @RequestParam String checkImg, User user, Model model)
			throws zdyException, Exception {
		
		
		User loginUser = userService.loginFindByUnameAndPwd(user.getUsername(), user.getPassword());
		if (loginUser == null) {
			User user1=user;
			user1.setPassword("");
			request.getSession().setAttribute("userinfo", user1);
			request.getSession().setAttribute("message", "密码输入错误请重新登陆");
			return "redirect:login.action";
		}
		
		String sessionCode = (String) request.getSession().getAttribute("checkcode");
		if (!sessionCode.equalsIgnoreCase(checkImg)) {
			request.getSession().setAttribute("userinfo", user);
			request.getSession().setAttribute("message", "验证码输入错误请重新注册");
			return "redirect:login.action";
		}
		request.getSession().setAttribute("loginUser", loginUser);
		request.getSession().removeAttribute("message");
		request.getSession().removeAttribute("userinfo");
		return "redirect:index.action";
	}

	// 用户退出
	@RequestMapping("userLogout")
	public String userLogout(HttpServletRequest request, HttpServletResponse response, Model model) {
		request.getSession().removeAttribute("loginUser");
		request.getSession().removeAttribute("shipinfo");
		response.setHeader("content-type", "text/html;charset=UTF-8");
		response.setHeader("refresh", "5;url=/shop/index.action");
		model.addAttribute("message", "退出成功，系统将在5秒之后跳转到首页......");
		return "msg";
	}

	// 进入管理员页面
	@RequestMapping("/admin")
	public String AdminIndex() {
		return "admin/index";
	}

	// admin/adminUser_login.action
	// adminUser_login管理员登录
	@RequestMapping("/adminUser_login")
	public String adminUser_login(HttpServletRequest request, Model model,@RequestParam String checkImg, @RequestParam String username,
			@RequestParam String password) throws Exception {
		
		Adminuser adminuname=userService.loginFindAdName(username);
		if (adminuname == null) {
			request.getSession().setAttribute("message", "该用户未被授予管理员身份!");
			return "redirect:admin.action";
		}
		Adminuser adminuserLogin = userService.adminUser_login(username, password);
		if (adminuserLogin == null) {
			Adminuser adu1=new Adminuser();
			adu1.setUsername(username);
			request.getSession().setAttribute("adu", adu1);
			request.getSession().setAttribute("message", "密码不正确请重新输入!");
			return "redirect:admin.action";
		}
		
		String sessionCode = (String) request.getSession().getAttribute("checkcode");
		if (!sessionCode.equalsIgnoreCase(checkImg)) {
			// model.addAttribute("message", "验证码输入错误请重新注册");
			// throw new zdyException("验证码输入错误请重新注册");
			request.getSession().setAttribute("adu", adminuserLogin);
			request.getSession().setAttribute("message", "验证码输入错误,请重新输入!");
			return "redirect:admin.action";
		}
		
		request.getSession().setAttribute("adminuserLogin", adminuserLogin);
		request.getSession().removeAttribute("message");
		request.getSession().removeAttribute("adu");
//		model.addAttribute("adminuserLogin", adminuserLogin);
		return "admin/home";
		
		
	}
	
	
	@RequestMapping(value = "/loginFindByid", produces = { "application/json;charset=UTF-8" }, method = RequestMethod.POST)
	@ResponseBody
	public ResponseMessage loginFindByid(HttpServletRequest request,
			 @RequestParam String username) throws Exception {
		User user = userService.loginFindByid(username);
		if (user!=null) {
			return ResponseMessage.success().addObject("zhuce", "可以登录");
		}else{
			return ResponseMessage.error().addObject("zhuce", "用户不存在");
		}
	}
	
	@RequestMapping(value = "/checkPsw", produces = { "application/json;charset=UTF-8" }, method = RequestMethod.POST)
	@ResponseBody
	public ResponseMessage checkPsw(HttpServletRequest request,
			 @RequestParam String password,@RequestParam String rePassword) throws Exception {
		if (password.equals(rePassword)) {
			return ResponseMessage.success().addObject("checkpsw", "正确!");
		}else{
			return ResponseMessage.error().addObject("checkpsw", "两次密码不同,请重新输入!");
		}
	}
	
	
	@RequestMapping(value = "/checkFindByid", produces = { "application/json;charset=UTF-8" }, method = RequestMethod.POST)
	@ResponseBody
	public ResponseMessage checkFindByid(HttpServletRequest request,@RequestParam String username) throws Exception {
		User user = userService.loginFindByid(username);
		if (user != null) {
			return ResponseMessage.success().addObject("checkuser", "用户存在!");
		}else{
			return ResponseMessage.error().addObject("checkuser", "用户不存在!");
		}
	}
}
